package com.example.waimai.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.waimai.common.CustomEX;
import com.example.waimai.mapper.CategoryMapper;
import com.example.waimai.pojo.Category;
import com.example.waimai.pojo.Dish;
import com.example.waimai.service.ServiceCategory;
import com.example.waimai.service.ServiceDish;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ServiceImplCategory extends ServiceImpl<CategoryMapper, Category> implements ServiceCategory {

    @Autowired
    private ServiceDish serviceDish;

    public void remove(Long id){
        LambdaQueryWrapper<Dish> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Dish::getCategoryId,id);
        long count = serviceDish.count(lqw);
        if(count>0){
            throw new CustomEX("当前分类管理了菜品，不能删除");
        }

        LambdaQueryWrapper<Category> lqw2=new LambdaQueryWrapper<>();
        lqw2.eq(Category::getSort, id);
        long count1 = serviceDish.count();
        if(count1>0){
            throw new CustomEX("关联了套餐，不能删除");
        }
        super.removeById(id);
    }
}
